<?php
class DF_MySQL extends Database
{
	private $connect, $select_db;
	
	# Server connection info
	const _SERVER	= 'localhost';
	const _USERNAME = 'username_here';
	const _PASSWORD = 'password_here';
	const _DATABASE = 'workingdb_here';
	
	/*------------------------------------------------
	 Constructor		
	--------------------------------------------------*/
	// not putting a constructor here b/c there is one in 
	// the extended abstract Database class
	
	/*------------------------------------------------
	 destructor
	--------------------------------------------------*/
	function __destruct ( )
	{
		$this->Close ( );
	}
	
	/*------------------------------------------------
	 Open database connection and select db
	--------------------------------------------------*/
	function Open ( )
	{
		$this->Connect ( );
		$this->SelectDb ( );
	}
	
	/*------------------------------------------------
	 Do the connecting
	--------------------------------------------------*/
	function Connect ( )
	{
		$this->connect = mysql_connect ( self::_SERVER, self::_USERNAME, self::_PASSWORD );
	}
	
	/*------------------------------------------------
	 Select the working database
	--------------------------------------------------*/
	function SelectDb ( )
	{
		if ( $this->connect ) mysql_select_db ( self::_DATABASE, $this->connect );
	}
	
	/*------------------------------------------------
	 Close the database connection
	--------------------------------------------------*/
	function Close ( )
	{
		if ( $this->connect && $this->select_db ) mysql_close ( $this->connect );
	}
	
	/*------------------------------------------------
	 Return any mysql errors for the current executing
	 query.
	--------------------------------------------------*/
	function Err ( )
	{
		return mysql_error ( );
	}
	
	/*------------------------------------------------
	 Return any mysql error numbers for the current 
	 executing query.
	--------------------------------------------------*/
	function Errno ( )
	{
		return mysql_errno ( );
	}
	
	/*------------------------------------------------
	 Run the query!
	 $_0: SQL string
	--------------------------------------------------*/
	function Query ( $_0 )
	{
		$sql = $_0;
		
		if ( $query = mysql_query ( $sql ) or die ( mysql_error ( ) ) ) 
		{
			return $query;
		}
		
		return FALSE;
	}
	
	/*------------------------------------------------
	 Return the ID of the last inserted row
	--------------------------------------------------*/
	function InsertId ( )
	{
		return mysql_insert_id ( );
	}
	
	/*------------------------------------------------
	 Count rows for the current query result resource
	 $_0: SQL string
	--------------------------------------------------*/
	function NumRows ( $_0 )
	{
		if ( $query = $this->Query ( $_0 ) ) 
		{
			return mysql_num_rows ( $query );
		}
		
		return FALSE;
	}
	
	/*------------------------------------------------
	 Get and return resulting row into an 
	 associative array
	 
	 $_0: SQL string
	--------------------------------------------------*/
	function FetchAssoc ( $_0 )
	{
		$assoc = array ( );
		
		if ( $query = $this->Query ( $_0 ) )
		{
			$assoc = mysql_fetch_assoc ( $query );
			
			mysql_free_result ( $query );
		}
		
		return $assoc;
	}
	
	/*------------------------------------------------
	 Get and return all resulting rows into an
	 associative array
	 $_0: SQL string
	--------------------------------------------------*/
	function FetchAssocRows ( $_0 )
	{
		$assoc = array ( );
		
		if ( $query = $this->Query ( $_0 ) )
		{
			
			while ( $row = mysql_fetch_assoc ( $query ) )
			{
				$assoc [ ] = $row;
			}
			
			mysql_free_result ( $query );
		}
		
		return $assoc;
	}
	
	/*------------------------------------------------
	 Return a mysql-safe version of a string
	 $_0: string to sanitize
	--------------------------------------------------*/
	function sanitize ( $_0 = NULL )
	{
		return trim ( mysql_real_escape_string ( $_0 ) );
	}
}
?>